package com.fg.cloud.common.message;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;
import java.util.Scanner;

/**
 * @author Administrator
 */
public class Speed {
	
	public static void main(String[] args) throws Exception {
		
		// 接收手机号
		Scanner input = new Scanner(System.in);
		System.out.print("您好！请输入您要发送的手机号：");
		String mobile = input.next();
		input.close();
		
		// 短信内容
		Random random = new Random();
		int nextInt = random.nextInt(10000);
		String content = "您的验证码是：" + nextInt + "。如需帮助请联系客服。";
		System.out.println(content);

		// 处理发送数据与路径
		StringBuffer sb = new StringBuffer();
		sb.append("account=fgdean");
		sb.append("&password=YUANda1234");
		sb.append("&mobile="+mobile);
		sb.append("&content="+java.net.URLEncoder.encode(content,"utf-8"));
		String postData = sb.toString();
		String postUrl = "http://sms.106jiekou.com/utf8/sms.aspx";
		
		// 发送成功
		String result = SMS(postData, postUrl);
		System.out.println(result);
		if ( "100".equals(result) ) {
			System.out.println("发送成功！");
		}
    }
	
	/**
	 * Description:发送短信
	 * @param mobile
	 * @param content
	 * @throws UnsupportedEncodingException
	 * @author dingjiang
	 * @date 2017年9月14日下午5:39:09
	 */
	public static String sendMsg(String mobile) throws UnsupportedEncodingException{
		// 短信内容
		Random random = new Random();
		int nextInt = random.nextInt(10000);
		String content = "您的验证码是：" + nextInt + "。如需帮助请联系客服。";
		System.out.println(content);
		
		// 处理发送数据与路径
		StringBuffer sb = new StringBuffer();
		sb.append("account=fgdean");
		sb.append("&password=YUANda1234");
		sb.append("&mobile="+mobile);
		sb.append("&content="+java.net.URLEncoder.encode(content,"utf-8"));
		String postData = sb.toString();
		String postUrl = "http://sms.106jiekou.com/utf8/sms.aspx";
		
		// 发送成功
		System.out.println(SMS(postData, postUrl));
		return String.valueOf(nextInt);
	}

    public static String SMS(String postData, String postUrl) {
        try {
            //发送POST请求
            URL url = new URL(postUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setUseCaches(false);
            conn.setDoOutput(true);

            conn.setRequestProperty("Content-Length", "" + postData.length());
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.write(postData);
            out.flush();
            out.close();

            //获取响应状态
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                System.out.println("connect failed!");
                return "";
            }
            //获取响应内容体
            String line, result = "";
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
            in.close();
            return result;
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
        return "";
    }
}
